<?php
/**
 * ServiceController
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: ServiceController.php 1223 2012-01-12 08:43:50Z maomao.thx@gmail.com $
 */
class ServiceController extends IG_Controller_Action
{
    public function __call($methodName, $args)
    {
        $action    = $this->getRequest()->getActionName();
        $names     = explode('-', $action);
        $module    = array_shift($names);
        $className = ucfirst($module) . '_Service_'
                   . implode('_', array_map('ucfirst', $names));

        if (!class_exists($className)) {
            parent::__call($methodName, $args);
        }

        //Create service
        $this->_wsdl = isset($_GET['wsdl'])?$_GET['wsdl']:null;
        $this->disableLayout();
        $this->setRender();

        $uri = 'http://' . $_SERVER['HTTP_HOST'] . "/service/$action?wsdl";
        if (isset($this->_wsdl)) {
            $wsdl = new Zend_Soap_AutoDiscover();
            $wsdl->setClass($className);
            //$wsdl->handle(); //php 5.3.0 bug不能使用

            $xml = $wsdl->toXml();
            if (!headers_sent()) {
                header('Content-Type: text/xml');
                header('Content-Length: ' . strlen($xml));
            }
            echo $xml;
        } else {
            $server = new Zend_Soap_Server($uri, array('soap_version' => SOAP_1_2));
            $server->setClass($className);
            //$server->handle();

            $xml = $server->setReturnResponse(true)->handle();
            header('Content-Length: ' . strlen($xml));
            echo $xml;
        }
    }

    public function activeAction()
    {
        $className = 'Ad_Service_Active';

        //Create service
        $this->_wsdl = isset($_GET['wsdl'])?$_GET['wsdl']:null;
        $this->disableLayout();
        $this->setRender();

        $uri = 'http://' . $_SERVER['HTTP_HOST'] . "/service/active/do/?wsdl";
        if (isset($this->_wsdl)) {
            $wsdl = new Zend_Soap_AutoDiscover();
            $wsdl->setClass($className);
            $wsdl->handle();
        } else {
            $server = new Zend_Soap_Server($uri, array('soap_version' => SOAP_1_2));
            $server->setClass($className);
            $server->handle();
        };
    }
}